using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 权限管理接口。
    /// 提供权限的增删改查相关 API。
    /// </summary>
    [ApiController]
    [Route("api/permissions")]
    [Authorize(Roles = "admin")]
    public class PermissionController : ControllerBase
    {
        /// <summary>
        /// 权限应用服务接口
        /// </summary>
        private readonly IPermissionAppService _service;
        
        /// <summary>
        /// 构造函数，注入权限服务
        /// </summary>
        /// <param name="service">权限应用服务接口</param>
        public PermissionController(IPermissionAppService service)
        {
            _service = service;
        }
        
        /// <summary>
        /// 获取所有权限信息
        /// </summary>
        /// <returns>权限列表</returns>
        [HttpGet]
        public async Task<IEnumerable<PermissionDto>> GetAll() => await _service.GetAllAsync();
        
        /// <summary>
        /// 根据主键获取权限信息
        /// </summary>
        /// <param name="id">权限主键</param>
        /// <returns>权限数据</returns>
        [HttpGet("{id}")]
        public async Task<ActionResult<PermissionDto>> GetById(long id)
        {
            var result = await _service.GetByIdAsync(id);
            if (result == null) return NotFound();
            return result;
        }
        
        /// <summary>
        /// 添加权限信息
        /// </summary>
        /// <param name="dto">权限数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public async Task<IActionResult> Add(PermissionDto dto)
        {
            await _service.AddAsync(dto);
            return Ok();
        }
        
        /// <summary>
        /// 更新权限信息
        /// </summary>
        /// <param name="id">权限主键</param>
        /// <param name="dto">权限数据传输对象</param>
        /// <returns>操作结果</returns>
        [HttpPut("{id}")]
        public async Task<IActionResult> Update(long id, PermissionDto dto)
        {
            await _service.UpdateAsync(id, dto);
            return Ok();
        }
        
        /// <summary>
        /// 删除权限信息
        /// </summary>
        /// <param name="id">权限主键</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(long id)
        {
            await _service.DeleteAsync(id);
            return Ok();
        }
    }
}
